﻿#region 程序集引用
using System;
using System.Web.Routing;
using System.Configuration;
#endregion

namespace Zhang.DeskTop
{
    public partial class BaseController : System.Web.Mvc.Controller
    {
        /// <summary>
        /// 分页大小
        /// </summary>
        private int _pageSize = 0;
        /// <summary>
        /// 当前页码
        /// </summary>
        private int _pageIndex = 0;

        /// <summary>
        /// 当前请求的分页页码,可为 Null
        /// </summary>
        protected int PageIndex
        {
            get { return this._pageIndex; }
            set { this._pageIndex = value; }
        }

        /// <summary>
        /// 分页大小
        /// </summary>
        protected int PageSize
        {
            get
            {
                this._pageSize = this._pageSize == 0 ? int.Parse(ConfigurationManager.AppSettings["ListPageSize"]) : this._pageSize;
                return this._pageSize;
            }
            set { this._pageSize = value; }
        }

        /// <summary>
        /// 当前查询条件的总记录数,第一次查询或是第一页请求时为 Null
        /// </summary>
        protected int TotalRecord = 0;

        protected override IAsyncResult BeginExecute(RequestContext requestContext, AsyncCallback callback, object state)
        {
            // 设置分页大小
            if (!string.IsNullOrEmpty(requestContext.HttpContext.Request.QueryString["pgSize"]) && Utils.OpString.IsNumber(requestContext.HttpContext.Request.QueryString["pgSize"]))
            {
                _pageSize = int.Parse(requestContext.HttpContext.Request.QueryString["pgSize"]);
                requestContext.HttpContext.Response.Cookies.Add(new System.Web.HttpCookie("ABSHOPPAGESIZE", _pageSize.ToString()));
            }
            else
            {
                if (requestContext.HttpContext.Request.Cookies["ABSHOPPAGESIZE"] != null && true == Utils.OpString.IsNumber(requestContext.HttpContext.Request.Cookies["ABSHOPPAGESIZE"].Value))
                {
                    _pageSize = int.Parse(requestContext.HttpContext.Request.Cookies["ABSHOPPAGESIZE"].Value);
                }
                else
                {
                    _pageSize = int.Parse(ConfigurationManager.AppSettings["ListPageSize"]);
                }
            }

            // 页码设置
            if (Utils.OpString.IsNumber(requestContext.HttpContext.Request.QueryString["rsCount"]) == false)
            {
                TotalRecord = 0;
            }
            else
            {
                TotalRecord = int.Parse(requestContext.HttpContext.Request.QueryString["rsCount"]);
            }

            if (Utils.OpString.IsNumber(requestContext.HttpContext.Request.QueryString["pg"]) == true)
            {
                _pageIndex = int.Parse(requestContext.HttpContext.Request.QueryString["pg"]);
                _pageIndex = _pageIndex < 1 ? 1 : _pageIndex;
            }
            else
            {
                _pageIndex = 1;
            }

            return base.BeginExecute(requestContext, callback, state);
        }
    }
}
